

from dzutil import me, now
import dzstore

from common.models.user import Users

class Owner(dzstore.Model):
    pass

class Item(dzstore.Model):
    def put(self):
        attributes = self.__dict__.keys()
        if 'date_added' not in attributes:
            self.time_added = now
        if 'creator' not in attributes:
            self.creator_id = me
        self.time_updated = now
        return dzstore.Model.put(self)

    def get_owners(self):
        return [owner.user_id for owner in Owner.find(item_id=self.id)]

    @classmethod
    def owned_by(cls,user_id):
        users_stuff = [item.item_id for item in Owner.find(user_id=user_id)]
        return [item for item in cls.get(users_stuff) if item]

# def make_test_data():
#     Owner.zap()
#     Note.zap()
#     Message.zap()
# 
#     Owner(user_id=me, item_id=Note(text='This is a note').put()).put()
#     Owner(user_id=me, item_id=Message(body='This is a message').put()).put()
#     Owner(user_id=me, item_id=Note(text='This is another note').put()).put()


if __name__  == '__main__':

    class Note(Item):
        pass

    class Message(Item):
        pass

    print Owner.all()
    print Note.all()
    print Message.all()

    print Note.get(131)











